package cn.b.sky.form.form.builder;

import cn.b.sky.form.model.Screen;
import cn.b.sky.form.model.Section;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * <strong>Title : FormBuilder.java<br></strong>
 * <strong>Package : cn.b.sky.form.form.builder<br></strong>
 * <strong>Description : </strong>表单HTML代码生成器<br> 
 * <strong>Create on : 2016年11月11日 下午4:14:42<br></strong>
 * <p>
 * @author ShiYing.Ke<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人|修改日期|修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
public class FormBuilder {

  public static String toHtmlElement(Screen screen) {

    StringBuilder formHtml = new StringBuilder();
    if (screen != null) {
      //formHtml.append("<div class=\"pageContent\"  layouth=\"42\">");
      //formHtml.append("<form method=\"post\" action=\"\" class=\"pageForm required-validate\" onsubmit=\"return validateCallback(this, dialogAjaxDone);\">");
      // 获取面板信息
      List<Section> sections = screen.getSections();
      if (!CollectionUtils.isEmpty(sections)) {
        // 根据sectionOrdinal排序
        Collections.sort(sections, new Comparator<Section>() {

          @Override
          public int compare(Section o1, Section o2) {
            return o1.getSectionOrdinal().compareTo(o2.getSectionOrdinal());
          }

        });
        // 循环获取面板
        for (Section sect : sections) {
          formHtml.append(PanelBuilder.toHtmlElement(sect));
        }
      }
      //	formHtml.append("</form>");
      //formHtml.append("</div>");
    }
    return formHtml.toString();
  }
}
